/**
	 The MIT License
	
	Copyright (c) <2012> <Jeonghoe Kim, http://kdarkdev.tistory.com, kdarkdev@gmail.com>
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
**/

/**
 * version 1.0 - 1st update (2012-05-18)
 */

/**
 * define package
 */
var com = (com == undefined)?{}:com;
com.kdarkdev = (com.kdarkdev == undefined)?{}:com.kdarkdev; 
com.kdarkdev.util = (com.kdarkdev.util == undefined)?{}:com.kdarkdev.util;

/**
 * Javascript SimpleDateFormat Class
	 yyyy(year) MM(month) dd(date) HH or hh(24hours/12hours) mm(minutes) ss(seconds)
	 
	 ex) alert(new com.kdarkdev.util.SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(new Date()));
 */
com.kdarkdev.util.SimpleDateFormat = function(formatString_) {
	if(formatString_ == undefined || formatString_ == '') {
		formatString = 'yyyyMMdd';
	} else {
		formatString = formatString_;
	}
	
	//private Method
	plusZero = function (value_, zeroNumber) {
		var zero = '';
		var value = value_.toString();
		if(value.length<zeroNumber) {
			for(var i=0; i<zeroNumber-value.length; i++) {
				zero+='0';
			}
			
		}
		return zero+value;
	};
};


/**
 * define method
 */
com.kdarkdev.util.SimpleDateFormat.prototype = {
	
	/**
	 * public function - Returns the date as specified format
	 * @param Date
	 * @return date String
	 */
	format: function(date){
		var dateRegex = /(y{0,4})([^yMdHhms]*)(M{0,2})([^yMdHhms]*)(d{0,2})([^yMdHhms]*)([Hh]{0,4})([^yMdHhms]*)(m{0,2})([^yMdHhms]*)(s{0,2})([^yMdHhms]*)/g;
		var dateRegexArray = dateRegex.exec(formatString);
		
		var yearFormat = dateRegexArray[1];
		var monthFormat = dateRegexArray[3];
		var dayFormat = dateRegexArray[5];
		var hourFormat = dateRegexArray[7];
		var minuteFormat = dateRegexArray[9];
		var secondFormat = dateRegexArray[11];
		
		var space1 = dateRegexArray[2].toString();
		var space2 = dateRegexArray[4].toString();
		var space3 = dateRegexArray[6].toString();
		var space4 = dateRegexArray[8].toString();
		var space5 = dateRegexArray[10].toString();
		var space6 = dateRegexArray[12].toString();
		
		var year = plusZero(date.getFullYear(), yearFormat.length);
		var month = plusZero(date.getMonth() + 1, monthFormat.length);
		var day = plusZero(date.getDate(), dayFormat.length);
		var hour = '';
		if (hourFormat == 'HH' || hourFormat == 'H') {
			hour = plusZero(date.getHours(), hourFormat.length);
		}
		else 
			if ((hourFormat == 'hh' || hourFormat == 'h') && date.getHours() > 12) {
				hour = plusZero(date.getHours() - 12, hourFormat.length);
			}
			else 
				if (hourFormat == 'hhHH') {
					hour = plusZero(date.getHours() - 12, hourFormat.length - 2) + plusZero(date.getHours(), hourFormat.length - 2);
				}
				else 
					if (hourFormat == 'HHhh') {
						hour = plusZero(date.getHours(), hourFormat.length - 2) + plusZero(date.getHours() - 12, hourFormat.length - 2);
					}
					else {
						hour = plusZero(date.getHours(), hourFormat.length);
					}
		
		var minute = plusZero(date.getMinutes(), minuteFormat.length);
		var second = plusZero(date.getSeconds(), secondFormat.length);
		
		var toDay = '';
		if (yearFormat.length > 0) {
			toDay += year;
		}
		if (space1.length > 0) {
			toDay += space1;
		}
		if (monthFormat.length > 0) {
			toDay += month;
		}
		if (space2.length > 0) {
			toDay += space2;
		}
		if (dayFormat.length > 0) {
			toDay += day;
		}
		if (space3.length > 0) {
			toDay += space3;
		}
		if (hourFormat.length > 0) {
			toDay += hour;
		}
		if (space4.length > 0) {
			toDay += space4;
		}
		if (minuteFormat.length > 0) {
			toDay += minute;
		}
		if (space5.length > 0) {
			toDay += space5;
		}
		if (secondFormat.length > 0) {
			toDay += second;
		}
		if (space6.length > 0) {
			toDay += space6;
		}
		
		return toDay;
	} //end format method
}; //end prototype
//class end